var app = getApp();
var common = require('../../utils/util.js');
const { $Toast } = require('../../dist/base/index');
var infoId;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:"",
    phone:"",
    address:"",
    default:false,
    region:[],
    pickerValue:"所在地区",
    btndis:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    infoId = options.id;
    //编辑地址
    if (options.id!=undefined){
      wx.setNavigationBarTitle({
        title: '编辑地址',
      })
      var that=this;
      wx.request({
        url: app.getUrl() + '/api/receive_info/receive_info',
        data: {
          infoId: options.id,
          Authorization: app.globalData.customerId + ":" + app.globalData.accessToken,
        },
        method: 'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded",
        },
        success(res) {
          if (res && res.statusCode == 200 && res.data && res.data.status == 200) {
            that.setData({
              name: res.data.obj.receiverName,
              phone: res.data.obj.receiverPhone,
              pickerValue:res.data.obj.receiverRegion,
              address: res.data.obj.receiverAddr,
              default: res.data.obj.isDefault,
            })
          } else {
            //token他处登录
            if (res.data.status == 403) {
              wx.showModal({
                title: '警告',
                content: '您的用户已在别的地方登录，是否重新登录？',
                success(res) {
                  if (res.confirm) {
                    wx.navigateTo({
                      url: '../login/login',
                    })
                  } else { }
                }
              })
            }
            // 没有登录状态
            else if (res.data.status == 406) {
              wx.switchTab({
                url: '../vipInfo/vipInfo',
                complete() {
                  wx.showToast({
                    title: '您还没有登录，请登录后查看',
                    icon: "none",
                    duration: 3000
                  })
                }
              })
            } else {
              $Toast({
                content: res.data.msg,
                type: 'warning',
                duration: 2,
              });
            }
          }
        },
        fail(res) {
          console.log(res)
          $Toast({
            content: '连接服务器失败',
            type: 'error',
            duration: 2,
          });
        }
      })
    }else{
      wx.setNavigationBarTitle({
        title: '新增地址',
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  areaChange(e){
    console.log(e.detail.value)
    this.setData({
      region: e.detail.value,
      pickerValue: e.detail.value[0] + e.detail.value[1] + e.detail.value[2]
    })
  },
  formSubmit: function (e) {
    if (common.isNull(e.detail.value.name)){
      $Toast({
        content: '请输入收货人姓名',
        type: 'warning',
        duration: 2,
      });
    } else if (common.isNull(e.detail.value.phone)){
      $Toast({
        content: '请输入手机号码',
        type: 'warning',
        duration: 2,
      });
    } else if (this.data.pickerValue=='所在地区') {
      $Toast({
        content: '请选择所在地区',
        type: 'warning',
        duration: 2,
      });
    } else if (common.isNull(e.detail.value.address)) {
      $Toast({
        content: '请输入详细地址',
        type: 'warning',
        duration: 2,
      });
    }else{
      this.setData({
        btndis:true
      })
      var that = this;
      //添加地址
      if (infoId==undefined){
        wx.request({
          url: app.getUrl() + '/api/receive_info/add_receive_info',
          data: {
            receiverName: e.detail.value.name,
            receiverPhone: e.detail.value.phone,
            receiverRegion: this.data.pickerValue,
            receiverAddr: e.detail.value.address,
            isDefault: e.detail.value.default,
            Authorization: app.globalData.customerId + ":" + app.globalData.accessToken,
          },
          method: 'POST',
          header: {
            "Content-Type": "application/x-www-form-urlencoded",
          },
          success(res) {
            that.setData({
              btndis: false
            })
            if (res && res.statusCode == 200 && res.data && res.data.status == 200) {
              wx.navigateBack({
                delta: 1,
                success() {
                  $Toast({
                    content: '修改成功',
                    type: 'success',
                    duration: 2,
                  });
                }
              })
            } else {
              //token他处登录
              if (res.data.status == 403) {
                wx.showModal({
                  title: '警告',
                  content: '您的用户已在别的地方登录，是否重新登录？',
                  success(res) {
                    if (res.confirm) {
                      wx.navigateTo({
                        url: '../login/login',
                      })
                    } else { }
                  }
                })
              }
              // 没有登录状态
              else if (res.data.status == 406) {
                wx.switchTab({
                  url: '../vipInfo/vipInfo',
                  complete() {
                    wx.showToast({
                      title: '您还没有登录，请登录后查看',
                      icon: "none",
                      duration: 3000
                    })
                  }
                })
              } else {
                $Toast({
                  content: res.data.msg,
                  type: 'warning',
                  duration: 2,
                });
              }
            }
          },
          fail(res) {
            that.setData({
              btndis: false
            })
            console.log(res)
            $Toast({
              content: '连接服务器失败',
              type: 'error',
              duration: 2,
            });
          }
        })
      }else{
        wx.request({
          url: app.getUrl() + '/api/receive_info/edit_receive_info',
          data: {
            infoId: infoId,
            receiverName: e.detail.value.name,
            receiverPhone: e.detail.value.phone,
            receiverRegion: this.data.pickerValue,
            receiverAddr: e.detail.value.address,
            isDefault: e.detail.value.default,
            Authorization: app.globalData.customerId + ":" + app.globalData.accessToken,
          },
          method: 'POST',
          header: {
            "Content-Type": "application/x-www-form-urlencoded",
          },
          success(res) {
            that.setData({
              btndis: false
            })
            if (res && res.statusCode == 200 && res.data && res.data.status == 200) {
              wx.navigateBack({
                delta: 1,
                success() {
                  $Toast({
                    content: '修改成功',
                    type: 'success',
                    duration: 2,
                  });
                }
              })
            } else {
              //token他处登录
              if (res.data.status == 403) {
                wx.showModal({
                  title: '警告',
                  content: '您的用户已在别的地方登录，是否重新登录？',
                  success(res) {
                    if (res.confirm) {
                      wx.navigateTo({
                        url: '../login/login',
                      })
                    } else { }
                  }
                })
              }
              // 没有登录状态
              else if (res.data.status == 406) {
                wx.switchTab({
                  url: '../vipInfo/vipInfo',
                  complete() {
                    wx.showToast({
                      title: '您还没有登录，请登录后查看',
                      icon: "none",
                      duration: 3000
                    })
                  }
                })
              } else {
                $Toast({
                  content: res.data.msg,
                  type: 'warning',
                  duration: 2,
                });
              }
            }
          },
          fail(res) {
            that.setData({
              btndis: false
            })
            console.log(res)
            $Toast({
              content: '连接服务器失败',
              type: 'error',
              duration: 2,
            });
          }
        })
      }
    }
  },
})